package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.common.util.MD5Encoder;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class CenterController {
	private static final String REDIRECTLOGIN = "redirect:/login";

	@Autowired
	private FootUserService footUserService;
	
	@RequestMapping(value = "/center", method = RequestMethod.GET)
	public String center(Model map, HttpServletRequest request) {
		FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
		if(user != null){
			map.addAttribute(Constants.USER, user);
		}
		return "center/myinfo";
	}
	
	@RequestMapping(value = "/center", method = RequestMethod.POST)
	public String submitcenter(@ModelAttribute FootUser footUser,
			BindingResult bindingResult, HttpServletRequest request) {
		if(footUser.getJoins() && StringUtils.isEmpty(footUser.getGameId())){
			bindingResult.rejectValue("gameId", Constants.MISFORMAT, "游戏ID不能为空");
		}
		FootUser user = footUserService.get(footUser.getId());
		if(!user.getName().equals(footUser.getName())){
			FootUser checkUser = footUserService.findUserByName(footUser.getName());
			if(checkUser != null){
                bindingResult.rejectValue("name", Constants.MISFORMAT, "此用户名已经注册,请更换");
			}
		}
		if (!bindingResult.hasErrors()) {
			if(StringUtils.isEmpty(footUser.getEmail())){
				user.setEmail(footUser.getEmail());
			}
			user.setGameId(footUser.getGameId());
			if(!user.getName().equals(footUser.getName())){
				user.setName(footUser.getName());
			}
			user.setIp(request.getRemoteAddr());
			user.setZhdlsj(new Date());
			user.setJoins(footUser.getJoins());
			footUserService.saveNewUser(user, request);
			return REDIRECTLOGIN;
		}else{
			return "center/myinfo";
		}
	}
	
	@RequestMapping(value = "/changepwd", method = RequestMethod.GET)
	public String changepwd() {
		return "center/changepwd";
	}
	
	@RequestMapping(value = "/changepwd", method = RequestMethod.POST)
	@ResponseBody
	public String submitchangepwd(HttpServletRequest request, String password, String newpwd) {
		FootUser user = (FootUser)request.getSession().getAttribute(Constants.USER);
		if(newpwd.length() > 20){
			return "length";
		}
		if(MD5Encoder.strToMD5(password).equals(user.getPassword())){
		    user.setPassword(newpwd);
			footUserService.updateNotNull(user, request);
			return Constants.OK;
		}else{
			return Constants.ERROR;
		}
	}
	
	@RequestMapping(value = "/findpwd", method = RequestMethod.GET)
	public String findpwd() {
		return "center/findpwd";
	}
	
	@RequestMapping(value = "/findpwd", method = RequestMethod.POST)
	@ResponseBody
	public String submitpwd(String userId, String qes1, String ans1, String qes2, String ans2, HttpServletRequest request) {
		FootUser user = footUserService.get(userId);
		if(user != null){
			user.setAns1(ans1);
			user.setAns2(ans2);
			user.setQes1(qes1);
			user.setQes2(qes2);
			footUserService.updateNotNull(user, request);
			return Constants.OK;
		}else{
			return Constants.ERROR;
		}
	}
}
